package com.ibeifeng.crm.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ibeifeng.crm.bean.Notice;
import com.ibeifeng.crm.service.NoticeService;

@Controller
@RequestMapping("/pages/notice/")
public class NoticeController {

	private Logger log = Logger.getLogger(NoticeController.class);
	@Resource
	NoticeService noticeService;

	@RequestMapping(value = "query", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String queryAll(HttpServletRequest request,
			HttpServletResponse response) {
			String pg = request.getParameter("page");
			String pgSize = request.getParameter("rows");
			// 若要显示的第几页为空，那么默认为1
			if (null == pg || "".equals(pg)) {
				pg = "1";
			}
			// 显示多少行为空，那么默认为10
			if (null == pgSize || "".equals(pgSize)) {
				pgSize = "10";
			}
			// 将从前台获得的分页参数放到findAllSplit查询分页的方法中
			List<Notice> customers = noticeService.findAllSplit(
					Integer.parseInt(pg), Integer.parseInt(pgSize));
			int count = noticeService.count();
			// 将从后台获得的course对象列表，以及总共查询多少个符合条件下的记录个数，以Map格式传到前台
			Map map = new HashMap();
			map.put("total", count);
			map.put("rows", customers);
			JSONArray obj = JSONArray.fromObject(map);
			String str = obj.toString();
			String js = str.substring(1, str.length() - 1);
			return js;
	}
	
	@RequestMapping(value = "querys", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String querys() {
			
			// 将从前台获得的分页参数放到findAllSplit查询分页的方法中
			List<Notice> customers = noticeService.queryAll();
			
			// 将从后台获得的course对象列表，以及总共查询多少个符合条件下的记录个数，以Map格式传到前台
			
			JSONArray obj = JSONArray.fromObject(customers );
			String str = obj.toString();
			String js = str.substring(1, str.length() - 1);
			return js;
	}
	
	@RequestMapping(value = "mquery", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String queryAll01(HttpServletRequest request,
			HttpServletResponse response) {
			String pg = request.getParameter("page");
			String pgSize = request.getParameter("rows");
			String keyWord=request.getParameter("keyWord");
			// 若要显示的第几页为空，那么默认为1
			if (null == pg || "".equals(pg)) {
				pg = "1";
			}
			// 显示多少行为空，那么默认为10
			if (null == pgSize || "".equals(pgSize)) {
				pgSize = "10";
			}
			// 将从前台获得的分页参数放到findAllSplit查询分页的方法中
			List<Notice> customers = noticeService.mquery(
					Integer.parseInt(pg), Integer.parseInt(pgSize),keyWord);
			int count = noticeService.count();
			// 将从后台获得的course对象列表，以及总共查询多少个符合条件下的记录个数，以Map格式传到前台
			Map map = new HashMap();
			map.put("total", count);
			map.put("rows", customers);
			JSONArray obj = JSONArray.fromObject(map);
			String str = obj.toString();
			String js = str.substring(1, str.length() - 1);
			return js;
	}

	@RequestMapping(value = "add")
	@ResponseBody
	public String add(Notice notice) {
		// sql执行情况，若大于0，那么则执行成功
		int result = noticeService.add(notice);
		// 若执行成功，则返回前台1,否则则返回0
		Map map = new HashMap();
		if (result > 0) {
			map.put("res", "true");
		} else {
			map.put("res", "false");
		}
		// 将执行结果存放在map中，以json格式返回前台
		JSONObject js = JSONObject.fromObject(map);
		return js.toString();
	}

	@RequestMapping(value = "update")
	@ResponseBody
	public String update(Notice notice) {
		// sql执行情况，若大于0，那么则执行成功
		int result = noticeService.update(notice);
		// 若执行成功，则返回前台1,否则则返回0
		Map map = new HashMap();
		if (result > 0) {
			map.put("res", "true");
		} else {
			map.put("res", "false");
		}
		JSONObject js = JSONObject.fromObject(map);
		System.out.println(js);
		return js.toString();
	}

	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(int noticeId) {
		// sql执行情况，若大于0，那么则执行成功
		Notice notice=new Notice();
		notice.setNoticeId(noticeId);
		
		int result = noticeService.delete(notice);
		// 若执行成功，则返回前台1,否则则返回0
		Map map = new HashMap();
		if (result > 0) {
			map.put("res", "true");
		} else {
			map.put("res", "false");
		}
		JSONObject js = JSONObject.fromObject(map);
		System.out.println(js);
		return js.toString();
	}

}
